#!/usr/bin/env python3
import fileinput
import sys

# args = [x.decode('unicode-escape') for x in sys.argv[1:]] # python 2
args = [str.encode(x).decode('unicode-escape') for x in sys.argv[1:]]
args = [x.encode('utf8') for x in args]

#for line in fileinput.input():
#for line in sys.stdin.readlines():
data = sys.stdin.buffer.read()
params = list(args)
while len(params) > 0:
    replacing = params[0] if len(params) >= 1 else b''
    replacement = params[1] if len(params) >= 2 else b''
    params = params[2:]
    if replacing == b'^':
      data = b'\n'.join([replacement + x for x in data.splitlines()]) + b'\n'
    elif replacing == b'$':
      data = b'\n'.join([x + replacement for x in data.splitlines()]) + b'\n'
    else:
      data = data.replace(replacing, replacement)
sys.stdout.buffer.write(data)

